home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte25 / ex9.c < prev    next >
C/C++ Source or Header  |  1995-04-23  |  3KB  |  69 lines

  1. #include <genstub.c>
  2.  
  3. // Child thread procedure that runs for ten seconds and exits.
  4. DWORD WINAPI ThreadProc(HWND hWnd)
  5. {
  6.    Sleep( 10000 );
  7.    ExitThread(TRUE);   // return to process
  8. }
  9.  
  10. // Main Window Procedure
  11. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  12. {
  13.    switch (uMsg)
  14.    {
  15.        case WM_COMMAND:       // process menu items
  16.                switch ( LOWORD( wParam )  )
  17.                {
  18.                   case IDM_TEST:
  19.                   {
  20.                      DWORD dwId;
  21.                      DWORD dwExitCode = FALSE;
  22.                      HANDLE hThread = CreateThread(NULL, 0, ThreadProc, hWnd, 0, &dwId );
  23.                      // while thread doesn't exit.
  24.                      WaitForInputIdle( GetCurrentProcess(), INFINITE );
  25.                      while (dwExitCode!=TRUE)
  26.                      {
  27.                         TCHAR szBuffer[128];
  28.                         WaitForSingleObject( hThread, 1000 );
  29.                         GetExitCodeThread( hThread, &dwExitCode );
  30.                         // Test exit code and print message.
  31.                         if (dwExitCode==STILL_ACTIVE)
  32.                            wsprintf(szBuffer, "Thread %lX is still running", dwId );
  33.                         else
  34.                            wsprintf(szBuffer, "Exit code of thread: %lX", dwExitCode);
  35.                         SendMessage( hWnd, WM_USER, 0, (LPARAM) szBuffer );
  36.                      }
  37.                   }
  38.                   break;
  39.                   case IDM_EXIT:
  40.                        DestroyWindow( hWnd );
  41.                        break;
  42.                }
  43.                break;
  44.        case WM_USER:
  45.                {  // Message to show synchronization actions.
  46.                   TCHAR szBuffer[101];
  47.                   static int row = 0;
  48.                   static int msg_num = 1;
  49.                   HDC hDC = GetDC( hWnd );
  50.                   FillMemory( szBuffer, 100, 32 );
  51.                   TextOut( hDC, 0, row, szBuffer, 100 );
  52.                   wsprintf( szBuffer, "%3d: %s", msg_num++, (LPTSTR)lParam );
  53.                   TextOut( hDC, 0, row, szBuffer, lstrlen( szBuffer ) );
  54.                   if ( row > 200 )
  55.                      row = 0;
  56.                   else
  57.                      row += 20;
  58.                   ReleaseDC( hWnd, hDC );
  59.                }
  60.                break;
  61.        case WM_DESTROY:
  62.                PostQuitMessage( 0 );
  63.                break;
  64.        default:
  65.                return DefWindowProc( hWnd, uMsg, wParam, lParam );
  66.    }
  67.    return NULL;
  68. }
  69.